<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>景点关键词词云分析</title>
    <script type="text/javascript" src="https://assets.pyecharts.org/assets/v5/echarts.min.js"></script>
    <script type="text/javascript" src="https://assets.pyecharts.org/assets/v5/echarts-wordcloud.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/jquery@3.6.0/dist/jquery.min.js"></script>
    <style>
        body {
            margin: 0;
            padding: 20px;
            background: linear-gradient(135deg, #f0f4f8 0%, #dfe9f3 100%);
            display: flex;
            justify-content: center;
            align-items: center;
            min-height: 100vh;
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            flex-direction: column;
        }
        .container {
            max-width: 1200px;
            width: 100%;
            display: flex;
            flex-direction: column;
            align-items: center;
        }
        .chart-container {
            width: 100%;
            height: 600px;
            background: white;
            border-radius: 15px;
            box-shadow: 0 12px 30px rgba(0, 0, 0, 0.18);
            overflow: hidden;
            transition: all 0.4s cubic-bezier(0.175, 0.885, 0.32, 1.1);
            position: relative;
        }
        .chart-container:hover {
            transform: translateY(-8px);
            box-shadow: 0 18px 40px rgba(0, 0, 0, 0.25);
        }
        .title {
            text-align: center;
            margin: 20px 0 5px;
            font-size: 32px;
            font-weight: 700;
            color: #2c3e50;
            letter-spacing: 1px;
            text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.08);
        }
        .subtitle {
            text-align: center;
            color: #5a6b7c;
            margin-bottom: 30px;
            font-size: 18px;
            max-width: 600px;
            line-height: 1.6;
        }
        .stats-container {
            width: 100%;
            background: white;
            border-radius: 15px;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.15);
            margin-top: 30px;
            padding: 25px;
            box-sizing: border-box;
        }
        .stats-title {
            text-align: center;
            font-size: 24px;
            margin: 0 0 20px;
            color: #2c3e50;
            font-weight: 600;
        }
        .stats-table {
            width: 100%;
            border-collapse: collapse;
            border-radius: 10px;
            overflow: hidden;
        }
        .stats-table th {
            background: linear-gradient(to right, #5470c6, #91cc75);
            color: white;
            font-weight: 600;
            padding: 16px 20px;
            text-align: center;
            font-size: 18px;
        }
        .stats-table td {
            padding: 14px 20px;
            text-align: center;
            border-bottom: 1px solid #eef1f5;
            font-size: 16px;
            transition: background 0.3s;
        }
        .stats-table tr:nth-child(even) {
            background-color: #f8fafd;
        }
        .stats-table tr:hover td {
            background-color: #e6f0ff;
            transform: scale(1.02);
            box-shadow: 0 4px 8px rgba(0,0,0,0.05);
        }
        .stats-table tr:hover {
            cursor: pointer;
        }
        .color-bar {
            height: 6px;
            border-radius: 3px;
            background: linear-gradient(90deg, #5470c6, #91cc75, #fac858, #ee6666, #73c0de);
            margin: 20px 0;
        }
        .loading-overlay {
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
            background: rgba(255, 255, 255, 0.9);
            display: flex;
            align-items: center;
            justify-content: center;
            flex-direction: column;
            z-index: 100;
            border-radius: 15px;
        }
        .loader {
            width: 60px;
            height: 60px;
            border: 5px solid rgba(84, 112, 198, 0.2);
            border-top: 5px solid #5470c6;
            border-radius: 50%;
            animation: spin 1s linear infinite;
            margin-bottom: 20px;
        }
        @keyframes spin {
            0% { transform: rotate(0deg); }
            100% { transform: rotate(360deg); }
        }
        .loading-text {
            font-size: 1.2rem;
            color: #5a6b7c;
            font-weight: 500;
        }
        .error-message {
            text-align: center;
            padding: 60px 20px;
            color: #ee6666;
        }
        .error-message i {
            font-size: 3rem;
            margin-bottom: 20px;
        }
        .error-message p {
            font-size: 1.2rem;
            margin-bottom: 15px;
        }
        .error-message button {
            background: linear-gradient(to right, #ee6666, #ff8e8e);
            color: white;
            border: none;
            padding: 10px 25px;
            border-radius: 30px;
            font-weight: bold;
            cursor: pointer;
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.3);
            transition: all 0.3s ease;
        }
        .error-message button:hover {
            transform: translateY(-3px);
            box-shadow: 0 8px 20px rgba(238, 102, 102, 0.5);
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="title">景点关键词云分析</div>
        <div class="subtitle">基于全国热门景点简介的词汇频率统计，展示核心旅游主题与文化特征</div>
        <div id="wordcloud-chart" class="chart-container">
            <div class="loading-overlay">
                <div class="loader"></div>
                <div class="loading-text">正在加载词云数据，请稍候...</div>
            </div>
        </div>

        <div class="stats-container">
            <div class="stats-title">高频词汇统计（前20位）</div>
            <div class="color-bar"></div>
            <div id="frequency-stats">
                <div class="loading-text">正在加载统计数据...</div>
            </div>
        </div>
    </div>

    <script>
        // 初始化图表
        var chart = echarts.init(
            document.getElementById('wordcloud-chart'), 'white', {renderer: 'canvas'});

        // 从后端获取词云数据
        function loadWordCloudData() {
            $.ajax({
                url: '/menu/get_word_cloud_data',
                type: 'GET',
                success: function(response) {
                    if (response.code === 0) {
                        // 处理数据并渲染图表
                        renderWordCloud(response.data);
                        // 更新高频词汇统计表
                        updateFrequencyStats(response.data);
                    } else {
                        showError('获取词云数据失败: ' + response.msg);
                    }
                    $('.loading-overlay').hide();
                },
                error: function(xhr, status, error) {
                    showError('请求失败: ' + error);
                    $('.loading-overlay').hide();
                }
            });
        }

        // 渲染词云图表
        function renderWordCloud(data) {
            // 优化后的配置项
            var option = {
                "backgroundColor": "#ffffff",
                "animation": true,
                "animationDuration": 1800,
                "animationEasing": "cubicOut",
                "color": [
                    "#5470c6", "#73c0de", "#91cc75", "#fac858", "#ee6666",
                    "#9a60b4", "#fc8252", "#3ba272", "#ea7ccc", "#19be6b"
                ],
                "tooltip": {
                    "show": true,
                    "trigger": "item",
                    "formatter": function(params) {
                        return `<strong>${params.name}</strong><br/>频次: ${params.value}`;
                    },
                    "backgroundColor": "rgba(255,255,255,0.95)",
                    "borderColor": "#5470c6",
                    "borderWidth": 1,
                    "textStyle": {
                        "color": "#2c3e50",
                        "fontSize": 16
                    },
                    "extraCssText": "box-shadow: 0 5px 20px rgba(0,0,0,0.15); border-radius: 8px; padding: 12px;"
                },
                "series": [{
                    "type": "wordCloud",
                    "shape": "star",
                    "rotationRange": [-30, 30],
                    "rotationStep": 15,
                    "girdSize": 10,
                    "sizeRange": [25, 110],
                    "drawOutOfBound": false,
                    "textStyle": {
                        "normal": {
                            "fontFamily": "'Microsoft YaHei', 'PingFang SC', sans-serif",
                            "fontWeight": "bold",
                            "color": function (params) {
                                return option.color[params.dataIndex % option.color.length];
                            },
                            "shadowBlur": 8,
                            "shadowColor": "rgba(0,0,0,0.15)"
                        },
                        "emphasis": {
                            "shadowBlur": 15,
                            "shadowColor": "#333",
                            "textBorderWidth": 2,
                            "textBorderColor": "#fff",
                            "transition": "all 0.3s"
                        }
                    },
                    "data": data,
                    "emphasis": {
                        "focus": "self",
                        "itemStyle": {
                            "shadowBlur": 20,
                            "shadowColor": "rgba(0, 0, 0, 0.3)"
                        }
                    }
                }]
            };

            // 设置配置项
            chart.setOption(option);

            // 响应式调整
            window.addEventListener('resize', function() {
                chart.resize();
            });
        }

        // 更新高频词汇统计表
        function updateFrequencyStats(data) {
            var frequencyStats = document.getElementById('frequency-stats');

            // 过滤无效词汇
            data = data.filter(item =>
                item.name.trim() !== "" &&
                item.name !== "\n" &&
                item.name !== " " &&
                item.name !== "未知"
            );

            // 按频数排序
            data.sort((a, b) => b.value - a.value);

            // 只取前20个
            var top20 = data.slice(0, 20);

            // 创建表格HTML
            var statsHTML = '<table class="stats-table">';
            statsHTML += '<tr><th>排名</th><th>关键词</th><th>出现频次</th></tr>';

            top20.forEach((item, index) => {
                // 为每行分配渐变色
                const colors = ["#5470c6", "#73c0de", "#91cc75", "#fac858", "#ee6666"];
                const colorIndex = index % colors.length;
                const bgColor = `${colors[colorIndex]}22`; // 添加透明度
                statsHTML += `
                <tr style="background: ${bgColor};">
                    <td>${index + 1}</td>
                    <td><strong>${item.name}</strong></td>
                    <td>${item.value}</td>
                </tr>`;
            });

            statsHTML += '</table>';
            frequencyStats.innerHTML = statsHTML;
        }

        // 显示错误信息
        function showError(message) {
            $('#wordcloud-chart').html(`
                <div class="error-message">
                    <i class="fas fa-exclamation-triangle"></i>
                    <p>${message}</p>
                    <button onclick="location.reload()">重新加载</button>
                </div>
            `);
        }

        // 页面加载完成后获取数据
        $(document).ready(function() {
            loadWordCloudData();
        });
    </script>
</body>
</html>